<template>
  <div class="mod-config">
    <el-dialog
      v-el-drag-dialog
      title="商品类型"
      :visible.sync="dialogVisible"
      width="80%">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
        <el-form-item>
          <el-input v-model="dataForm.key" placeholder="参数名" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDataList()">查询</el-button>
          <el-button v-if="isAuth('goods:class:add')" type="primary" @click="updateHandle(null)">新增</el-button>
          <el-button v-if="isAuth('goods:class:remove')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
        </el-form-item>
      </el-form>
      <el-table
        :data="dataList"
        border
        v-loading="dataListLoading"
        style="width: 100%;height: 550px;overflow-y: inherit;">
        <el-table-column
          prop="classId"
          header-align="center"
          align="center"
          label="ID">
        </el-table-column>
        <el-table-column
          prop="className"
          header-align="center"
          align="center"
          label="分类名">
          <template slot-scope="scope">
            {{scope.row.className}}
              <el-button type="primary" size="mini" style="float: right;" plain @click="parentClassId = scope.row.classId
              openSubClassHandle(scope.row.classId)">查看下级分类</el-button>
          </template>
        </el-table-column>
        <el-table-column
          prop="typeName"
          header-align="center"
          align="center"
          label="类型">
        </el-table-column>
        <el-table-column
          prop="display"
          header-align="center"
          align="center"
          label="显示">
          <template slot-scope="scope">
            {{scope.row.display === 1 ? '是' : '否'}}
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
    </el-dialog>

    <el-dialog
      title="查看"
      :visible.sync="subClassListVisible"
      width="30%">
      <el-table
        :data="subClassList"
        @selection-change="selectionChangeHandle"
        border
        style="width: 100%;height: 500px;overflow-y: inherit;">
        <el-table-column
          type="selection"
          header-align="center"
          align="center"
          width="50">
        </el-table-column>
        <el-table-column
          prop="id"
          label="ID">
        </el-table-column>
        <el-table-column
          prop="className"
          label="分类名">
        </el-table-column>
        <el-table-column
          prop="display"
          header-align="center"
          align="center"
          label="显示">
          <template slot-scope="scope">
            {{scope.row.display === 1 ? '是' : '否'}}
          </template>
        </el-table-column>
      </el-table>
      <el-row :gutter="10">
        <el-col :offset="15" style="margin-top: 15px">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" :disabled="dataListSelections.length === 0"  @click="dataSubmit">确 定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    data () {
      return {
        dataForm: {
          keywords: ''
        },
        parentClassId: 0,
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        subClassList: [],
        subClassListVisible: false,
        dialogVisible: false,
        specsListVisible: false,
        goodsType: [],
        goodsTypeId: 0,
        addSpecsList: [],
        addSpecsVisible: false
      }
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dialogVisible = true
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/goods/class/queryGoodsClassList'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'keywords': this.dataForm.keywords
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.data.list
            this.totalPage = data.data.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      openSubClassHandle (id) {
        this.$http({
          url: this.$http.adornUrl(`/goods/class/queryGoodsSubClassById/${id}`),
          method: 'get'
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.subClassList = data.data
            this.subClassListVisible = true
          }
        })
      },
      dataSubmit () {
        let data = this.dataListSelections.map(data => { return data.id })
        if (data.length !== 1) {
          this.$message.error('只能选择一个商品分类')
          return
        }
        this.$emit('addGoodsClass', {data: data[0], showData: '商品分类：' + this.dataListSelections[0].className})
        this.dialogVisible = false
        this.subClassListVisible = false
      }
    }
  }
</script>
